package com.zhou.test.spring.secrity.security.athentication;

import cn.hutool.extra.servlet.ServletUtil;
import com.zhou.common.lib.model.Response;
import com.zhou.common.lib.util.JSONUtils;
import com.zhou.common.operator.log.core.constant.OperateActionEnum;
import com.zhou.common.operator.log.core.model.OperateLogInfo;
import com.zhou.common.operator.log.core.model.PrincipalInfo;
import com.zhou.common.operator.log.core.resolver.ClientInfoResolver;
import com.zhou.common.operator.log.core.resolver.PrincipalResolver;
import com.zhou.common.operator.log.core.service.OperateLogStorageService;
import com.zhou.test.spring.secrity.content.UserContext;
import com.zhou.test.spring.secrity.entity.SysUser;
import com.zhou.test.spring.secrity.model.security.MyUserDetails;
import com.zhou.test.spring.secrity.service.impl.SysUserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * Describe: 自定义 Security 用户未登陆处理类
 * Author: 就 眠 仪 式
 * CreateTime: 2019/10/23
 */
@Slf4j
@Component
public class SecureAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    public static final String JSON_UTF8 = "application/json;charset=UTF-8";

    @Resource
    private OperateLogStorageService logService;

    @Resource
    private PrincipalResolver principalResolver;
    @Resource
    private ClientInfoResolver clientInfoResolver;

    @Autowired
    private SysUserServiceImpl sysUserService;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
        log.info("登录成功");
        MyUserDetails currentUser = UserContext.currentUser();
        //更新最后一次访问时间
        SysUser sysUser = new SysUser();
        sysUser.setUserId(currentUser.getSysUser().getUserId());
        sysUser.setLoginDate(LocalDateTime.now());
        sysUserService.updateById(sysUser);

        OperateLogInfo logInfo = new OperateLogInfo();
        logInfo.setClientInfo(this.clientInfoResolver.resolveClientInfo(request));
        logInfo.setAddTime(new Date());

        PrincipalInfo principalInfo = new PrincipalInfo();
        principalInfo.setUserId(currentUser.getUserId() + "");
        principalInfo.setUserName(currentUser.getUsername());
        principalInfo.setUserPhone(currentUser.getPhone());
        principalInfo.setOrgId(currentUser.getCurrOrgId() + "");
        principalInfo.setOrgName(currentUser.getCurrOrgName() + "");
        logInfo.setPrincipal(principalInfo);
        logInfo.setAction(OperateActionEnum.LOGIN.getAction());
        logInfo.setOperatingInstruction("登录成功:用户名密码");
//        logInfo.setOperatingSystem(operateLog.operateSystem());
//        logInfo.setOperatingModule(opreateModule);
//        logInfo.setOperatingType(operateLog.operateType());
//        logInfo.setResource(resource);
        this.logService.append(logInfo);

        Response success = Response.success();
        ServletUtil.write(response, JSONUtils.toJson(success), JSON_UTF8);
    }
}
